home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-02-26 | 49.6 KB | 1,918 lines | [TEXT/KAHL] |
- // Text Media Sample Code
- //
- // sample to show how to do searches with text media, and how to use
- // a simple text proc to get the text (without the style info) out and
- // to display it in a window.
- //
- // This is something of a work in progress, it contains a few bugs, if you find
- // anthing that you feel needs fixing, or explaining, please let me know
- // AppleLink: NICKT
- //
- //
- // Modification History:
- //
- // 5/16/94 nick added text media stuff, repeat text in box below movie, uses TextBox
- // 5/17/94 nick implement find dialog
- // 5/31/94 nick implement textedit stuff to allow editing of the text track
- // 6/1/94 nick clean up textedit stuff and allow user to edit the text track
- // 8/10/94 nick support for styled text, bug fixes
- // 9/29/94 nick support for core appleevents
- // 10/13/94 nick cleaned up for release
- // fix up cut and paste for text edit commands
- // added TE edit menu
- // 10/14/94 nick final clean up, move adjust menus to just before menukey or menuselect
- //
- //
- // to do:
- // make the edit box scrollable, implement font menus, style and size mwnus for text
- //
- // Copyright: © 1992-4 by Apple Computer, Inc.
-
- #include <FixMath.h>
-
- #include "mtb.h"
- #include "aevt.h"
- #include "TextMediaExample.h"
- #include "QuickTimeUtils.h"
-
- extern Boolean gQuitFlag = false ; // set to true when we want to quit
-
- const RGBColor kWindowBGColor = {0xFF00, 0xFF00, 0xFF00} ; // grays made the caret yellow, ughh, need to investigate this
- const RGBColor kRGBBlack = {0x0000, 0x0000, 0x0000} ;
- const RGBColor kRGBGray = {0xAAAA, 0xAAAA, 0xAAAA} ;
- const RGBColor kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF} ;
-
- const Rect kDefaultWinRect ;
- const short kSlopMargin = 8 ;
-
- Boolean gDlgSearchBack = false ;
- Boolean gDlgSearchWrap = false ;
- Str255 gDlgSearchText = "\p" ;
-
- //---------------------------------------------------------------------
-
- DocumentHandle CreateDocumentHandle( WindowPtr aWindow )
- {
- DocumentHandle theHandle ;
-
- theHandle = (DocumentHandle)NewHandle( sizeof(DocumentRecord)) ;
- if( theHandle != nil ) {
-
- // make sure we init the doc record fields to sensible values.
-
- (**theHandle).myController = nil ;
- (**theHandle).myTEH = nil ;
- (**theHandle).myFindButton = nil ; // this is the find button for this window
- (**theHandle).myUpdateButton = nil ; // allows the user to update the movie text track
- (**theHandle).myMovieIsActive = true ; // either the movie is or the te field is.
- (**theHandle).myTextHasBeenModified = false ;
-
- // store a reference to the document record for this window
- // in the refcon field of this window.
- SetWRefCon ( aWindow, (long)theHandle );
-
- }
-
- return theHandle ;
- }
-
- //---------------------------------------------------------------------
-
- WindowPtr CreateMovieWindow( Rect *aRect, Str255 theTitle )
- {
- RGBColor nattyGray = kWindowBGColor ;
- GrafPtr savedPort ;
- WindowPtr aWindow ;
-
- GetPort( &savedPort ) ;
-
- // create a window for the movie
- if((aWindow = NewCWindow(nil,
- aRect,
- theTitle,
- false,
- noGrowDocProc,
- (WindowPtr)-1,
- true,
- 0 )) != nil )
- {
- // set our port to the newly created movie, make
- // the content area gray.
- SetPort( aWindow );
- RGBBackColor( &nattyGray ) ;
- }
-
- // restore current port
-
- SetPort( savedPort );
- return aWindow ;
- }
-
- //---------------------------------------------------------------------
-
- MovieController SetUpMovieWindowWithController( WindowPtr aWindow, Movie aMovie )
- {
- MovieController aController; // the controller for this movie
- Rect aRect ; // bounding rect of the movie
- GrafPtr savedPort ; // used to reset the current port
- DocumentHandle aDocHdl ;
- short movieWidth ;
- short movieHeight ;
-
- aDocHdl = (DocumentHandle)GetWRefCon( aWindow ) ;
-
- // save whatever the port was before we go in here
- GetPort( &savedPort ) ;
-
- DoPrerollMovie( aMovie ) ;
-
- // sanity check
- if( aMovie == nil )
- CheckError( paramErr, "\pSetUpMovieWindowWithController was passed nil for aMovie") ;
-
- SetPort( (GrafPtr)aWindow ) ;
-
- // set the movies environment to the current
- // port and gDevice (pass nil for defaults)
- SetMovieGWorld( aMovie, nil, nil ) ;
-
- // Use the movie bounding rect to size the movie
- GetMovieBox( aMovie, &aRect );
-
- // lets make sure the movie bounds are reasonable
- movieWidth = aRect.right - aRect.left ;
- movieHeight = aRect.bottom - aRect.top ;
- aRect.top = aRect.left = 0 ;
- aRect.right = movieWidth ;
- aRect.bottom = movieHeight ;
-
- OffsetRect( &aRect, kSlopMargin, kSlopMargin ) ;
-
- SetMovieBox( aMovie, &aRect );
-
- // create a movie controller for the movie
- aController = NewMovieController (aMovie, &aRect, 0L | mcTopLeftMovie | mcWithBadge );
-
- // should have some error handling here
- if (aController == nil)
- ExitToShell();
- else
- (**aDocHdl).myController = aController ;
-
- SetPort( savedPort ) ;
- return aController ;
- }
-
- //---------------------------------------------------------------------
-
- OSErr SetMovieTextHandler( WindowPtr aWindow )
- {
- MediaHandler aMediaHandler ;
- MovieController aController ;
- Movie aMovie ;
- Track aTrack ;
- DocumentHandle aDocHdl ;
-
- aDocHdl = (DocumentHandle)GetWRefCon( aWindow ) ;
- aController = (**aDocHdl).myController ;
-
- if( aController != nil ) {
- aMovie = MCGetMovie( aController ) ;
- }
- else {
- CheckError( -1, "\pCouldn't get movie controller") ;
- }
- // set the text handling procedure - we only want
- // to do this is there is not a text track in the movie
-
- if((aTrack = GetFirstTrackOfType( aMovie, TextMediaType )) != nil )
- {
- // we need the duration of the text track
- // load the text track into ram
- LoadTrackIntoRam( aTrack, 0L, GetTrackDuration( aTrack ), 0L ) ;
-
- aMediaHandler = GetMediaHandler( GetTrackMedia( aTrack ) ) ;
- if( aMediaHandler != nil ) {
- SetTextProc( aMediaHandler, NewTextMediaProc(MyTextProc), (long)aWindow ) ;
- }
- }
- return GetMoviesError() ;
- }
-
- //---------------------------------------------------------------------
-
- OSErr AddTEAndControl( WindowPtr aWindow )
- {
- Rect aRect ;
- Rect controlRect ;
- MovieController aController ;
- Movie aMovie ;
- GrafPtr savedPort ;
- ControlHandle aControl ;
- DocumentHandle aDocHdl ;
-
- short movieWindowWidth ;
- short movieWindowLength ;
-
- aDocHdl = (DocumentHandle)GetWRefCon( aWindow ) ;
- aController = (**aDocHdl).myController ;
-
- // Get the rect for the movie controller (includes the movie)
-
- if( aController != nil ) {
- // get the bounding rect of the movie plus the controller
- CheckError( MCGetControllerBoundsRect(aController, &aRect), "\pMCGetControllerBoundsRect") ;
- }
- else {
- CheckError( -1, "\pCouldn't get movie controller") ;
- }
-
- InsetRect ( &aRect, -kSlopMargin, -kSlopMargin );
-
- GetPort( &savedPort ) ;
- SetPort( aWindow ) ;
-
-
- // allow 125 pixels for us to display the text, the update text btn, and the find button
- aRect.bottom += 125 ;
-
- // we use these elsewhere so calculate this stuff now
- movieWindowWidth = aRect.right - aRect.left ;
- movieWindowLength = aRect.bottom - aRect.top ;
-
- SizeWindow (aWindow, movieWindowWidth, movieWindowLength, true);
-
- // create a TE record
- {
- short h, v ;
- Rect theRect ;
-
- // get the bounds of the window
- theRect = ((GrafPtr)aWindow)->portRect ;
-
- // we will always want 115 pixels from the bottom of the window
- v = theRect.bottom - 115 ;
- h = theRect.right - theRect.left ;
-
- // get the bounds of the box to draw in
- theRect.top = v ;
- theRect.bottom = v + 25 ;
- theRect.left = 0 ;
- theRect.right = h ;
-
- InsetRect( &theRect, kSlopMargin, 0 ) ;
-
-
- (**aDocHdl).myTEH = TEStyleNew( &theRect, &theRect ) ;
- }
-
- // create the button for updating the text
-
- if(( aControl = GetNewControl( 130, aWindow )) == nil )
- ExitToShell() ;
-
- controlRect = (**aControl).contrlRect ;
-
- {
- short h ;
- short v ;
-
- h = (aRect.right - aRect.left) - (controlRect.right - controlRect.left) - 16 ;
- v = (aRect.bottom - aRect.top) - (controlRect.bottom - controlRect.top) - 16 ;
-
- // locate the control so that its bottom right corner is 16 x 16 pixels from
- // the bottom r.h. corner of the window. This is a good technique to use
- // as it would allow easy localization of the control title in the resource,
- // which may alter the size of the control.
-
- MoveControl( aControl, h, v ) ;
-
- // store the reference to the control in the document record
- (**aDocHdl).myUpdateButton = aControl ;
- }
-
- // create the button to do the searches
-
- if(( aControl = GetNewControl( 128, aWindow )) == nil )
- ExitToShell() ;
-
- controlRect = (**aControl).contrlRect ;
-
- {
- short h ;
- short v ;
-
- h = (aRect.right - aRect.left) - (controlRect.right - controlRect.left) - 16 ;
- v = (aRect.bottom - aRect.top) - (controlRect.bottom - controlRect.top) - (16 + 35);
-
- // locate the control so that its bottom right corner is 16 x (16 + 35) pixels from
- // the bottom r.h. corner of the window. This is a good technique to use
- // as it would allow easy localization of the control title in the resource,
- // which may alter the size of the control.
-
- MoveControl( aControl, h, v ) ;
-
- // store the reference to the control in the document record
- (**aDocHdl).myFindButton = aControl ;
- }
-
- // move the window to {50,50} on the main device and show it
- MoveWindow ( aWindow, 50, 50, true );
-
- // the next command ensures the window is aligned to a longword
- // boundary, which will ensure premium performance for playback
- // *** need to update the drag code ***
- AlignWindow( aWindow, false, &aRect, nil ) ;
-
- SetPort( savedPort ) ;
-
- }
-
- //---------------------------------------------------------------------
-
- void DoCreateMovieWindow( Movie aMovie )
- {
- Rect aRect = kDefaultWinRect ;
- Rect controlRect ; // for the push button control
- WindowPtr aWindow;
- Track aTrack ;
- ControlHandle aControl ;
- MovieController aController ;
- DocumentHandle theDocHandle ;
- GrafPtr savedPort ;
-
-
- GetPort( &savedPort ) ;
-
- // create the window
- aWindow = CreateMovieWindow( &aRect, "\pA Movie" ) ;
- SetPort( (GrafPtr)aWindow ) ;
-
- if( aWindow == nil )
- CheckError( -1, "\pCouldn't create window") ;
-
- // set up our document handle in the refcon field of the window
- theDocHandle = CreateDocumentHandle( aWindow ) ;
- if( theDocHandle == nil )
- CheckError( MemError(), "\pCouldn't allocate memory") ;
-
-
- // set up the movie for the window
- aController = SetUpMovieWindowWithController( aWindow, aMovie ) ;
-
- // set up the text handler proc for the movie
- CheckError( SetMovieTextHandler( aWindow ), "\pCouldn't set the text handler for the movie window") ;
-
- // size the window for the movie and add the TE record and the controls
- AddTEAndControl( aWindow ) ;
-
- // finally show the window.
- ShowWindow ( aWindow );
-
- //
- InvalRect( &((GrafPtr)aWindow)->portRect ) ;
-
- // set up the edit menu
- MCEnableEditing( aController, true ) ;
-
- SetPort( savedPort ) ;
-
- }
-
-
- //---------------------------------------------------------------------
-
- void DoDestroyMovieWindow( WindowPtr aWindow )
- {
- Movie aMovie ;
- MovieController aController ;
- DocumentHandle aDocHdl ;
-
- aDocHdl = (DocumentHandle)GetWRefCon( aWindow ) ;
- aController = (**aDocHdl).myController ;
-
- if( aController != nil ) {
- aMovie = MCGetMovie( aController ) ;
- if( aMovie != nil ) {
- DisposeMovie (aMovie);
- }
- DisposeMovieController (aController);
- }
-
- DisposeWindow(aWindow);
- }
-
- //---------------------------------------------------------------------
-
- void DoUpdateText( WindowPtr whichWindow, Point theMouse, ControlHandle aControl )
- {
- Track aTrack ;
- MediaHandler aMediaHandler ;
- Movie aMovie ;
- OSErr theErr ;
- DocumentHandle aDocHdl;
- MovieController aController ;
- GrafPtr savedPort ;
-
- GetPort( &savedPort ) ;
- SetPort( (GrafPtr)whichWindow ) ;
-
- // update the text track for the movie.
- aDocHdl = (DocumentHandle)GetWRefCon( whichWindow ) ;
- aController = (**aDocHdl).myController ;
-
- aMovie = MCGetMovie(aController ) ;
-
- if( TrackControl(aControl, theMouse, nil ) != 0 ) {
-
- // get the text media track
- if((aTrack = GetFirstTrackOfType( aMovie, TextMediaType )) != nil )
- {
- TimeValue currentTime ;
- TimeValue interestingTime ;
- TimeValue theDuration ;
-
- TEHandle aTEH ;
- Media aMedia ;
-
- CheckError( GetMoviesError(), "\pfailed after GetFirstTrackOfType");
- aMedia = GetTrackMedia( aTrack ) ;
- aMediaHandler = GetMediaHandler( aMedia ) ;
-
- CheckError( GetMoviesError(), "\pfailed after GetMediaHandler");
-
- if( aMediaHandler != nil ) {
-
- Fixed width ;
- Fixed height ;
- Rect trackBounds = {0, 0, 0, 0};
- TimeValue sampleTime ;
-
- TimeValue mediaSampleDuration ;
- TimeValue mediaSampleStartTime ;
- TimeValue mediaCurrentTime ;
- long mediaSampleIndex ;
-
- // get a handle to the text
- aTEH = (**aDocHdl).myTEH ;
-
- // get the current movie time
- currentTime = GetMovieTime( aMovie, nil ) ;
- if( currentTime < 0 )
- CheckError( -1, "\pthis is an empty edit") ;
-
- // we need to find the start and duration for this media sample
- // first get the time in media time of the current sample
-
- mediaCurrentTime = TrackTimeToMediaTime( currentTime, aTrack ) ;
-
- // then get detailed information about the start and duration
- // of the current sample, this is used later
-
- MediaTimeToSampleNum ( aMedia,
- mediaCurrentTime,
- &mediaSampleIndex,
- &mediaSampleStartTime,
- &mediaSampleDuration);
-
- // look back and find where this text starts
- GetTrackNextInterestingTime( aTrack, nextTimeEdgeOK | nextTimeMediaSample, currentTime,
- -kFix1, &interestingTime, nil ) ;
-
- currentTime = interestingTime ;
-
- GetTrackNextInterestingTime( aTrack, nextTimeEdgeOK | nextTimeMediaSample, currentTime,
- kFix1, &interestingTime, &theDuration ) ;
-
- // tell the media that we are about to edit stuff
- theErr = BeginMediaEdits( aMedia ) ;
- if( theErr != noErr )
- return ;
-
- // delete whatever was there before
- theErr = DeleteTrackSegment( aTrack, interestingTime, theDuration ) ;
- if( theErr != noErr )
- return ;
-
- // get the track bounds
- GetTrackDimensions( aTrack, &width, &height ) ;
- trackBounds.right = Fix2Long ( width );
- trackBounds.bottom = Fix2Long ( height );
-
-
- // and write out the new data to the media
- theErr = AddTESample( aMediaHandler,
- aTEH,
- &((CGrafPtr)(**aTEH).inPort)->rgbBkColor,
- (**aTEH).just, // use the justification from the TE record
- &trackBounds,
- dfClipToTextBox,
- 0,
- 0,
- 0,
- nil,
- mediaSampleDuration,
- &sampleTime) ;
-
- if( theErr != noErr )
- return ;
-
- // insert the new media into the track
- theErr = InsertMediaIntoTrack( aTrack,
- interestingTime,
- sampleTime,
- mediaSampleDuration,
- kFix1) ;
-
- theErr = EndMediaEdits( aMedia ) ;
- if( theErr != noErr )
- return ;
- }
-
- InvalRect( &((GrafPtr)whichWindow)->portRect );
- }
- }
-
- SetPort( savedPort ) ;
- return ;
- }
-
-
- //---------------------------------------------------------------------
-
- void HandleContentClick( WindowPtr whichWindow, EventRecord *theEvent )
- {
- GrafPtr oldPort ;
- Boolean handled = false;
- DocumentHandle aDocHdl ;
- TEHandle myTEHdl ;
- Boolean shiftDown = false ;
- short controlPart ;
- ControlHandle aControl ;
- MovieController aController ;
- Boolean isMyMovieActive ;
- Boolean savedActive ;
-
- ControlHandle findBtn ;
- ControlHandle updateBtn ;
-
- GetPort(&oldPort ) ;
- SetPort( whichWindow );
-
- aDocHdl = (DocumentHandle)GetWRefCon( whichWindow ) ;
-
- if( aDocHdl == nil )
- return ;
-
- findBtn = (**aDocHdl).myFindButton ;
- updateBtn = (**aDocHdl).myUpdateButton;
-
- if( whichWindow != FrontWindow() )
- SelectWindow ( whichWindow );
-
- GlobalToLocal( &theEvent->where ) ;
- // did we click on the control?
- controlPart = FindControl( theEvent->where, whichWindow, &aControl ) ;
- switch( controlPart ) {
- case inButton:
- if( aControl == findBtn ) {
- DoFindDialog( whichWindow, theEvent->where, aControl) ;
- handled = true ;
- }
- else if( aControl == updateBtn ) {
- DoUpdateText( whichWindow, theEvent->where, aControl) ;
- handled = true ;
- }
- break ;
- }
-
- if( !handled )
- {
-
- if( aDocHdl != nil ) {
-
- Rect theRect ;
-
- // set up for hit testing
- myTEHdl = (**aDocHdl).myTEH ;
- aController = (**aDocHdl).myController ;
- savedActive = isMyMovieActive = (**aDocHdl).myMovieIsActive ;
-
- if(myTEHdl == nil || aController == nil )
- return ;
-
- theRect = (**myTEHdl).viewRect ;
-
- // if the TE field is not already active, make it so
- if( PtInRect( theEvent->where, &theRect ) ) {
-
- (**aDocHdl).myMovieIsActive = false ;
-
- // if the movie was playing, make it stop
- MCDoAction( aController, mcActionPlay, nil ) ;
- }
-
- // check to see if we clicked in the movie
- // if the movie is not active make it so
- // otherwise see if the click was in the TEField
-
- if(!isMyMovieActive) {
- if(IsPointInMovieController( aController, whichWindow, theEvent->where)) { ;
- (**aDocHdl).myMovieIsActive = true ;
- }
- else {
- if( PtInRect( theEvent->where, &theRect ) ) {
-
- // if the movie is not active, let te process it
- TEClick(theEvent->where, theEvent->modifiers & shiftKey, myTEHdl) ;
- }
- }
- }
- }
- }
-
- if(whichWindow == FrontWindow()) {
- DoSelectActive( whichWindow ) ;
- }
-
- // the update event handler handles toggling the
- // highlight around the active area, this can be
- // either the movie active or the textedit field
-
- if( savedActive != (**aDocHdl).myMovieIsActive )
- InvalRect( &((GrafPtr)whichWindow)->portRect ) ;
-
- SetPort( oldPort ) ;
- }
-
- //---------------------------------------------------------------------
-
- void MyDoIdle( WindowPtr whichWindow )
- {
- GrafPtr oldPort ;
- DocumentHandle aDocHdl ;
- TEHandle myTEHdl ;
-
- GetPort(&oldPort ) ;
- SetPort( whichWindow );
-
- // did we click in the TE field
- aDocHdl = (DocumentHandle)GetWRefCon( whichWindow ) ;
- if( aDocHdl != nil ) {
- myTEHdl = (**aDocHdl).myTEH ;
- if( myTEHdl != nil ) {
- TEIdle( myTEHdl ) ;
- }
- }
-
- SetPort( oldPort ) ;
-
- }
-
- //---------------------------------------------------------------------
-
- void DoActivateWindow( WindowPtr whichWindow, Boolean becomingActive)
- {
- DocumentHandle aDocHdl ; // our document record is stuffed in the window refcon
- ControlHandle updateControlHandle,
- findControlHandle ; // this is the find button
- TEHandle myTEH ;
- MovieController aController ;
- GrafPtr savedPort ;
-
- GetPort(&savedPort) ;
- SetPort((GrafPtr)whichWindow) ;
-
- aDocHdl = (DocumentHandle)GetWRefCon(whichWindow) ;
- if( aDocHdl != nil)
- {
- findControlHandle = (**aDocHdl).myFindButton ;
- updateControlHandle = (**aDocHdl).myUpdateButton ;
- myTEH = (**aDocHdl).myTEH ;
- aController = (**aDocHdl).myController ;
-
- if( findControlHandle != nil && myTEH != nil)
- {
- if( becomingActive ) {
- HiliteControl( findControlHandle, 0 ) ;
-
- if( (**aDocHdl).myTextHasBeenModified )
- HiliteControl( updateControlHandle, 0 ) ;
- else
- HiliteControl( updateControlHandle, 255 ) ;
-
- DoSelectActive( whichWindow ) ;
- AdornWindow( whichWindow, &kRGBBlack ) ;
- }
- else {
- Rect aFramingRect = (**myTEH).viewRect ;
-
- HiliteControl( updateControlHandle, 255 ) ;
- HiliteControl( findControlHandle, 255 ) ;
- AdornWindow( whichWindow, &kRGBGray ) ;
- MCActivate( aController, whichWindow, false ) ;
- TEDeactivate( myTEH );
-
- InvalRect( &aFramingRect ) ;
- }
- }
- }
-
- SetPort(savedPort) ;
- }
-
- void DoSelectActive( WindowPtr whichWindow )
- {
-
- // make sure that the movie and the text edit field are activated in
- // an appropriate manner. We may either have the movie or the
- // text edit field selected, if the TE is selected make sure we "do the
- // right thing", if it is the movie make sure the controller is active.
- DocumentHandle aDocHdl ;
- Boolean movieIsActive ;
- TEHandle aTEH ;
- MovieController aController ;
-
- if( (aDocHdl = (DocumentHandle)GetWRefCon(whichWindow)) == nil)
- return ;
-
- aTEH = (**aDocHdl).myTEH ;
- movieIsActive = (**aDocHdl).myMovieIsActive ;
- aController = (**aDocHdl).myController ;
-
- if( aTEH == nil || aController == nil)
- return ;
-
- if( movieIsActive ) {
- Rect aFramingRect = (**aTEH).viewRect ;
-
- MCActivate( aController, whichWindow, true ) ;
- MCDoAction( aController, mcActionSetKeysEnabled, (void *)true) ;
- TEDeactivate( aTEH );
-
- InvalRect( &aFramingRect ) ;
- }
- else {
- MCActivate( aController, whichWindow, false ) ;
- MCDoAction( aController, mcActionSetKeysEnabled, (void *)false) ;
- TEActivate( aTEH ) ;
- }
-
-
- }
-
- //---------------------------------------------------------------------
- // AdornWindow assumes the port is set correctly...
- void AdornWindow( WindowPtr whichWindow, const RGBColor *adornerColor )
- {
- DocumentHandle aDocHdl ;
- Boolean movieIsActive ;
- TEHandle aTEH ;
- MovieController aController ;
- Rect aFramingRect ;
- RGBColor color ;
-
- // get the foreground color
- GetForeColor(&color);
-
- if( (aDocHdl = (DocumentHandle)GetWRefCon(whichWindow)) == nil)
- return ;
-
- if( (**aDocHdl).myMovieIsActive ) {
-
- if(( aController = (**aDocHdl).myController) == nil)
- return ;
-
- // get the movie bounding box & adorn it
- MCGetControllerBoundsRect( aController, &aFramingRect ) ;
- InsetRect( &aFramingRect, -6, -6 ) ;
- PenSize( 2, 2 ) ;
- RGBForeColor( adornerColor ) ;
- FrameRect( &aFramingRect ) ;
- PenNormal() ;
-
- }
- else {
-
- if(( aTEH = (**aDocHdl).myTEH) == nil )
- return ;
-
- // adorn the TERec, the user will know it is inactive,
- // because the caret is inactive.
- aFramingRect = (**aTEH).viewRect ;
- InsetRect( &aFramingRect, -2, -2 ) ;
- PenSize( 1, 1 ) ;
- GetForeColor(&color);
- RGBForeColor( adornerColor ) ;
- FrameRect( &aFramingRect ) ;
- PenNormal() ;
-
- }
-
- // restore the foreground color
- RGBForeColor(&color);
- }
-
-
- //---------------------------------------------------------------------
-
- void DoUpdateWindow( WindowPtr whichWindow )
- {
- GrafPtr savedPort ;
- ControlHandle theUpdateControl = (**((DocumentHandle)GetWRefCon(whichWindow))).myUpdateButton ;
-
- GetPort(&savedPort ) ;
- SetPort( whichWindow );
-
- BeginUpdate (whichWindow);
- EraseRgn( whichWindow->visRgn ) ;
-
- // check to see if the update control should be active
- if( (**((DocumentHandle)GetWRefCon(whichWindow))).myTextHasBeenModified )
- HiliteControl( theUpdateControl, 0 ) ;
- else
- HiliteControl( theUpdateControl, 255 ) ;
-
- // draw the controls
- DrawControls( whichWindow ) ;
-
- // draw the current text
- DoDrawCurrentText( whichWindow ) ;
-
- // finally adorn the selection, we may either have the movie or the
- // text edit field selected, frame the selection, if it is the movie
- // make sure the controller is active.
-
- AdornWindow( whichWindow, &kRGBBlack ) ;
-
- EndUpdate (whichWindow);
- SetPort( savedPort ) ;
-
- }
-
- //---------------------------------------------------------------------
-
- void MainEventLoop( void )
- {
- OSErr err;
- EventRecord theEvent;
- WindowPtr whichWindow, theWindow;
- short part;
- short controlPart ;
- ControlHandle aControl ;
- Boolean wasMovieEvent ;
- MovieController aController ;
- Point aPoint = {100, 100};
- Rect refreshArea ;
- Rect screenRect ;
- GrafPtr oldPort ;
-
- DocumentHandle aDocHdl ;
- Boolean myMovieIsActive ;
-
-
- // use this in calls to MCDoAction
- long myMCActionParams ;
-
- while (!gQuitFlag) {
-
- WaitNextEvent(everyEvent, &theEvent, 0, nil );
-
- wasMovieEvent = false ;
-
- // make sure we have an active front window
- if((theWindow = FrontWindow()) != nil)
- {
- // let the movie controller deal with the edit menu
- aDocHdl = (DocumentHandle)GetWRefCon( theWindow ) ;
- if( aDocHdl != nil )
- {
- MovieController aController = (**aDocHdl).myController ;
-
- myMovieIsActive = (**aDocHdl).myMovieIsActive ;
-
- if( aController != nil )
- {
- // The edit menu - let the movie controller setting it up
- MenuHandle editMenu = GetMHandle ( mEdit ) ;
- MCSetUpEditMenu(aController, theEvent.modifiers, editMenu ) ;
- }
- }
-
- // if the event is a mousedown and the movies is not active in the front
- // window we want to do our "normal" mouseDown handling and make the movie
- // active.
- //
- // the next statement looks a little wierd, what we are actually saying here is
- //
- // "if the following is NOT true:
- //
- // the event is a mounse down AND the current movie is not active (i.e. the
- // textEdit field is active - this is an either/or situation)
- //
- // then
- //
- // let the movie controller handle it
- //
- // otherwise
- //
- // let it fall thru to the main event loop and let the mouse down handler
- // deal with it.
- //
-
- if( !(theEvent.what == mouseDown && !myMovieIsActive))
- {
- for( theWindow = FrontWindow(); theWindow != nil ; theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow)
- {
- // get the movie associated with the window
- if(( aDocHdl = (DocumentHandle)GetWRefCon( theWindow )) != nil) {
- aController = (**aDocHdl).myController ;
-
-
- // check we have a valid controller, and
- // if so, see if the controller can handle
- // this event
-
- if( aController != nil ) {
-
- wasMovieEvent = MCIsPlayerEvent(aController, &theEvent) ;
- }
- }
- }
- }
- }
-
-
- // if the controller didn't or couldn't handle it
- // we need to handle it in the main event loop.
-
- if (!wasMovieEvent) {
-
- switch (theEvent.what) {
-
- case nullEvent:
- if(( whichWindow = FrontWindow()) != nil )
- MyDoIdle( whichWindow ) ;
- break ;
-
- case mouseDown:
-
- part = FindWindow (theEvent.where, &whichWindow);
-
- switch (part) {
-
- case inMenuBar:
- AdjustMenus() ;
- HandleMenuCommand(MenuSelect(theEvent.where));
- break;
-
- case inDrag:
- {
- Rect r;
- Movie aMovie ;
- MovieController aController ;
- DocumentHandle aDocHdl ;
-
- aDocHdl = (DocumentHandle)GetWRefCon( whichWindow ) ;
- aController = (**aDocHdl).myController ;
-
- aMovie = MCGetMovie( aController ) ;
-
-
- GetMovieBox(aMovie, &r);
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragAlignedWindow(whichWindow, theEvent.where, &screenRect, &r, nil);
- }
- break;
-
- case inContent:
-
- HandleContentClick( whichWindow, &theEvent ) ;
- break ;
-
- case inGoAway:
-
- // if the window got closed, dispose of our movie
- // and the controller and the window.
-
- if( TrackGoAway (whichWindow, theEvent.where ))
- DoDestroyMovieWindow( whichWindow ) ;
-
- break;
- }
- break ;
-
- case updateEvt:
-
- whichWindow = (WindowPtr)theEvent.message;
- DoUpdateWindow( whichWindow ) ;
- break;
-
- case keyDown:
- case autoKey:
- HandleKeyPress(&theEvent);
- break;
-
- case diskEvt:
- if ( HiWrd(theEvent.message) != noErr )
- (void) DIBadMount(aPoint, theEvent.message);
- break;
-
- case activateEvt:
- // so are we becoming active?
- whichWindow = (WindowPtr)theEvent.message ;
- if( IsAppWindow(whichWindow)) {
- DoActivateWindow( whichWindow, ((theEvent.modifiers & activeFlag) != 0)) ;
- }
- break;
-
- case osEvt:
- switch ((theEvent.message >> 24) & 0x00FF) { // High byte of message
- case suspendResumeMessage:
- if (FrontWindow()) {
- DoActivateWindow(FrontWindow(), !((theEvent.message & resumeFlag) == 0));
- }
- break;
-
- case mouseMovedMessage:
- break;
- }
- break;
-
- case kHighLevelEvent: // Let the Apple Event Manager handle high level event.
- AEProcessAppleEvent(&theEvent);
- break;
-
- }
- }
- }
- }
-
- //---------------------------------------------------------------------
-
- Boolean IsAppWindow(WindowPtr window)
- {
- short windowKind;
-
- if ( window == nil )
- return false;
- else {
- windowKind = ((WindowPeek) window)->windowKind;
- return ((windowKind >= userKind) || (windowKind == dialogKind));
- }
- }
-
- //---------------------------------------------------------------------
-
- // this handles posting the find dialog
- void DoFindDialog( WindowPtr theMovieWindow, Point theMouse, ControlHandle aControl )
- {
- DialogPtr theFindDialog ;
- short itemHit ;
- short iType ;
- Handle iHandle ;
- Rect iRect ;
- Track aTrack ;
- MediaHandler aMediaHandler ;
- Movie aMovie ;
-
- if( TrackControl(aControl, theMouse, nil ) != 0 ) {
-
- // get the find dialog
- theFindDialog = GetNewDialog(129, nil, (WindowPtr)-1 ) ;
-
- // set the default item, cancel item, and signal this has a TE field
- SetDialogDefaultItem( theFindDialog, 1 ) ;
- SetDialogCancelItem( theFindDialog, 2 ) ;
- SetDialogTracksCursor( theFindDialog, true ) ;
-
- // set up the default for the backwards search checkbox
- GetDItem ( theFindDialog, diBackwardsCheckBox, &iType, &iHandle, &iRect );
- SetCtlValue ( (ControlHandle)iHandle, gDlgSearchBack ) ;
-
- // set up the default for the wrap search checkbox
- GetDItem ( theFindDialog, diWrapCheckBox, &iType, &iHandle, &iRect );
- SetCtlValue ( (ControlHandle)iHandle, gDlgSearchWrap ) ;
-
- // and restore and setting we had for the search string
- GetDItem ( theFindDialog, diTextBox, &iType, &iHandle, &iRect );
- SetIText ( iHandle, gDlgSearchText );
- SelIText ( theFindDialog, diTextBox, 0, 32767 );
-
- do {
-
- ModalDialog(nil, &itemHit) ;
-
- if( itemHit == diBackwardsCheckBox ) {
- GetDItem ( theFindDialog, diBackwardsCheckBox, &iType, &iHandle, &iRect );
- gDlgSearchBack = !gDlgSearchBack ;
- SetCtlValue ( (ControlHandle)iHandle, gDlgSearchBack ) ;
- }
- else if( itemHit == diWrapCheckBox ) {
- GetDItem ( theFindDialog, diWrapCheckBox, &iType, &iHandle, &iRect );
- gDlgSearchWrap = !gDlgSearchWrap ;
- SetCtlValue ( (ControlHandle)iHandle, gDlgSearchWrap ) ;
- }
- } while( itemHit != ok && itemHit != cancel) ;
-
- if( itemHit == ok ) {
- // process the result
- GetDItem ( theFindDialog, diTextBox, &iType, &iHandle, &iRect );
- GetIText ( iHandle, gDlgSearchText );
- }
-
- // and dispose of the dialog
- DisposDialog ( theFindDialog );
-
- // if we hit cancel or if the user blanked out the text and hit return
- // return, we don't want to search. If the user did blank it out
- // then beep.
- if( itemHit != ok || gDlgSearchText[0] == '\0') {
- if( gDlgSearchText[0] == '\0' )
- SysBeep( 10 ) ;
- return ;
- }
-
- }
- else
- return ;
-
- // if we are here it is safe to assume that we have text to search for
- // get the movie reference from the window.
-
- // do the search
- DoSearchForStringInMovieWindow( theMovieWindow, gDlgSearchText, gDlgSearchBack, gDlgSearchWrap) ;
-
- }
-
- //---------------------------------------------------------------------
-
- void DoSearchForStringInMovieWindow( WindowPtr theMovieWindow,
- Str255 SearchText,
- Boolean searchBack,
- Boolean searchWrap)
- {
- Movie aMovie ;
- Track aTrack ;
- MediaHandler aMediaHandler ;
- MovieController aController ;
- DocumentHandle aDocHdl ;
-
- aDocHdl = (DocumentHandle)GetWRefCon( theMovieWindow ) ;
- aController = (**aDocHdl).myController ;
-
- aMovie = MCGetMovie( aController ) ;
-
- if((aTrack = GetFirstTrackOfType( aMovie, TextMediaType )) != nil )
- {
- CheckError( GetMoviesError(), "\pfailed after GetFirstTrackOfType");
- aMediaHandler = GetMediaHandler( GetTrackMedia( aTrack ) ) ;
-
- CheckError( GetMoviesError(), "\pfailed after GetMediaHandler");
-
- if( aMediaHandler != nil ) {
- long searchFlags ;
- TimeValue startTime = 0,
- foundTime,
- foundDuration ;
- long offset ;
- TimeRecord newTime ;
- RGBColor highlightColor ;
-
- if(searchBack)
- searchFlags = findTextReverseSearch ;
- else
- searchFlags = 0L ;
-
- if(searchWrap)
- searchFlags = searchFlags | findTextWrapAround ;
-
- CheckError( FindNextText( aMediaHandler,
- (Ptr)(&SearchText[1]),
- SearchText[0],
- searchFlags,
- GetMovieTime( aMovie, nil ),
- &foundTime,
- &foundDuration,
- &offset ), "\pFindNextText failed"); ;
-
- if(foundTime != -1) {
- // we need to convert the timevalue to a time record.
- newTime.value.hiLong = 0 ;
- newTime.value.loLong = foundTime ;
- newTime.scale = GetMovieTimeScale( aMovie ) ;
- newTime.base = nil ;
-
- // tell the movie to go to this time
- MCDoAction(aController, mcActionGoToTime, &newTime);
-
- // highlight the text
- highlightColor = (**((GrafVars **)((CGrafPtr)theMovieWindow)->grafVars)).rgbHiliteColor;
-
- HiliteTextSample( aMediaHandler,
- foundTime,
- offset,
- offset + SearchText[0],
- &highlightColor);
-
- }
- }
- }
- }
-
-
- //---------------------------------------------------------------------
-
- void AdjustMenus( void )
- {
- WindowPtr theWindow;
- MenuHandle menu;
- long offset;
- Boolean undo;
- Boolean cutCopyClear;
- Boolean paste;
- TEHandle te;
- MovieController aController = nil ;
- DocumentHandle aDocHdl = nil;
-
-
-
- if((theWindow = FrontWindow()) != nil ) {
- aDocHdl = (DocumentHandle)GetWRefCon(theWindow) ;
- aController = (**aDocHdl).myController ;
- }
-
- menu = GetMHandle ( mFile ) ;
- if( theWindow != nil ) {
- EnableItem ( menu, iClose );
- EnableItem ( menu, iSaveAs );
- }
- else {
- DisableItem ( menu, iClose );
- DisableItem ( menu, iSaveAs );
- }
-
- menu = GetMHandle(mEdit);
-
- if( theWindow == nil || aController == nil || aDocHdl == nil) {
- DisableItem ( menu, 0 );
- }
- else if( (**aDocHdl).myMovieIsActive) {
- // is the movie the frontmost item
- // then let the movie controller set the menus up
-
- // this is done with MCSetUpEditMenu in the main event loop
- EnableItem( menu, 0 );
-
- }
- else {
-
- undo = false;
- cutCopyClear = false;
- paste = false;
-
- if ( IsDAWindow(theWindow) ) {
-
- undo = true; // all editing is enabled for DA windows
- cutCopyClear = true;
- paste = true;
-
- } else if ( IsAppWindow(theWindow) ) {
-
- te = (**aDocHdl).myTEH ;
- if ( (*te)->selStart < (*te)->selEnd )
- cutCopyClear = true;
-
- // Cut, Copy, and Clear is enabled for app. windows with selections
- if ( GetScrap(nil, 'TEXT', &offset) > 0)
- paste = true; // if there’s any text in the clipboard, paste is enabled
- }
- if ( undo )
- EnableItem(menu, iUndo);
- else
- DisableItem(menu, iUndo);
- if ( cutCopyClear ) {
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- } else {
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iClear);
- }
- if ( paste )
- EnableItem(menu, iPaste);
- else
- DisableItem(menu, iPaste);
- }
- }
-
-
- //---------------------------------------------------------------------
-
- void HandleKeyPress(EventRecord *event)
- {
- char key;
- DocumentHandle aDocHdl ;
- TEHandle myTEHdl ;
- WindowPtr aWindow ;
- GrafPtr savedPort ;
-
- key = event->message & charCodeMask;
-
- if ( event->modifiers & cmdKey ) { /* Command key down? */
- AdjustMenus() ;
- HandleMenuCommand(MenuKey(key));
- }
- else {
- if((aWindow = FrontWindow()) != nil ) {
-
- GetPort( &savedPort ) ;
- SetPort( aWindow ) ;
-
- // get the document handle
- if( (aDocHdl = (DocumentHandle)GetWRefCon(aWindow)) == nil )
- return ;
-
- // check the text edit field is active, if not beep and return
- if( (**aDocHdl).myMovieIsActive == true ) {
- SysBeep( 10 );
- return ;
- }
-
- // get the teh
- if((myTEHdl = (**aDocHdl).myTEH) == nil)
- return ;
-
- // let textedit process the keydown
- TEKey( key, myTEHdl);
-
- // and signal that the text has changed
- if( !(**aDocHdl).myTextHasBeenModified ) {
- Rect theControlRect ;
-
- (**aDocHdl).myTextHasBeenModified = true ;
-
- // make the control redraw by invalidating it's rect
- theControlRect = (**((**aDocHdl).myUpdateButton)).contrlRect ;
- InvalRect( &theControlRect ) ;
- }
-
- SetPort( savedPort ) ;
- }
- }
- }
-
-
- //---------------------------------------------------------------------
-
- void HandleMenuCommand(long menuResult)
- {
- short menuID;
- short menuItem;
- Str255 daName;
- DialogPtr theDialog ;
- short itemHit ;
- FSSpec theFile ;
- OSErr err ;
- short theRef ;
-
- Movie aMovie = nil;
- MovieController aController ;
-
- StandardFileReply theSFReply ;
- DocumentHandle aDocHdl ;
- WindowPtr aWindow ;
- OSErr theErr ;
-
-
- menuID = HiWrd(menuResult);
- menuItem = LoWrd(menuResult);
- switch ( menuID ) {
- case mApple:
- switch ( menuItem ) {
- case iAbout:
- theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
- SetDialogDefaultItem(theDialog, 1) ;
-
- do {
- ModalDialog ( nil, &itemHit );
- } while( itemHit != ok ) ;
- DisposDialog ( theDialog );
- break;
-
- default:
- GetItem(GetMHandle(mApple), menuItem, daName);
- (void) OpenDeskAcc(daName);
- break;
- }
- break;
-
- case mFile:
- switch ( menuItem ) {
- case iOpen:
-
- // get the movie to open
-
- if(GetAMovieFileSpec(&theFile)) {
- SendOpenDoc(&theFile) ;
- }
-
- break ;
-
- case iClose:
- DoDestroyMovieWindow( FrontWindow() ) ;
- break ;
-
- case iSaveAs:
-
-
- // get the name of the file to save as
- StandardPutFile("\pSave movie as:", "\pNew Movie File", &theSFReply) ;
-
- if( theSFReply.sfGood ) {
-
- // get the movie for this window
- aDocHdl = (DocumentHandle)GetWRefCon( FrontWindow() ) ;
- aController = (**aDocHdl).myController ;
-
- if( aController != nil ) {
- aMovie = MCGetMovie( aController ) ;
- }
- else {
- CheckError( -1, "\pCouldn't get movie controller") ;
- }
-
- if( aMovie != nil ) {
-
- FlattenMovieData( aMovie, flattenAddMovieToDataFork, &theSFReply.sfFile, 'TVOD', smSystemScript, createMovieFileDeleteCurFile ) ;
-
- theErr = GetMoviesError() ;
- CheckError( theErr, "\pCouldn't FlattenMovieData") ;
-
- }
-
- }
- break ;
-
- case iQuit:
- SendQuitApp() ; // send ourselves a quit event
- break;
- }
- break;
-
- case mEdit:
-
- if((aWindow = FrontWindow()) != nil ) {
- aDocHdl = (DocumentHandle)GetWRefCon(aWindow) ;
- aController = (**aDocHdl).myController ;
- }
-
- // is the movie the current item, or is the textedit field current
- if((**aDocHdl).myMovieIsActive) {
-
- // the movie is active so we use the movie controller
- // cut and paste commands
-
- switch ( menuItem ) {
-
- case iUndo:
- MCUndo( aController ) ;
- break ;
-
- case iCut:
- aMovie = MCCut( aController ) ;
- break ;
-
- case iCopy:
- aMovie = MCCopy( aController ) ;
- break ;
-
- case iPaste:
- MCPaste( aController, nil ) ;
- break ;
-
- case iClear:
- MCClear( aController ) ;
- break ;
-
- case iSelectAll:
- break ;
- }
- if( aMovie ) {
- PutMovieOnScrap( aMovie, 0) ;
- DisposeMovie( aMovie ) ;
- }
- }
- else {
- GrafPtr savedPort ;
-
- GetPort(&savedPort) ;
- SetPort( (GrafPtr)aWindow ) ;
-
- // the textedit field is active - handle cut and paste
-
- if ( !SystemEdit(menuItem-1) ) {
-
- TEHandle te = (**aDocHdl).myTEH;
-
- switch ( menuItem ) {
-
- case iCut:
- if ( (theErr = ZeroScrap()) == noErr ) {
- TECut(te);
- if ( (theErr = TEToScrap()) != noErr ) {
- ZeroScrap();
- }
- }
- (**aDocHdl).myTextHasBeenModified = true ;
- CheckError(theErr, "\pCan't perform textEdit cut operation" ) ;
- break;
-
- case iCopy:
- if ( (theErr = ZeroScrap()) == noErr ) {
- TECopy(te); // after copying, export the TE scrap
- if ( (theErr = TEToScrap()) != noErr ) {
- ZeroScrap();
- }
- }
- CheckError(theErr, "\pCan't perform textEdit copy operation" ) ;
- break;
-
- case iPaste: // import the TE scrap before pasting
- if ( (theErr = TEFromScrap()) == noErr ) {
- if ( !(TEGetScrapLen() + ((*te)->teLength - ((*te)->selEnd - (*te)->selStart)) > 32000 )) {
- Handle aHandle = (Handle) TEGetText(te);
- short oldSize = GetHandleSize(aHandle);
-
- // allow 1k slop in the newSize to make textEdit happy
- short newSize = oldSize + TEGetScrapLen() + 1024;
- SetHandleSize(aHandle, newSize);
- theErr = MemError();
- SetHandleSize(aHandle, oldSize);
- if (theErr == noErr)
- TEStylPaste(te);
- }
- }
- (**aDocHdl).myTextHasBeenModified = true ;
- CheckError(theErr, "\pCan't perform textEdit paste operation" ) ;
- break;
-
- case iClear:
- TEDelete(te);
- (**aDocHdl).myTextHasBeenModified = true ;
- break;
- }
- }
- if((**aDocHdl).myTextHasBeenModified) {
- InvalRect( &((GrafPtr)aWindow)->portRect ) ;
- }
- SetPort( savedPort ) ;
- }
- break ;
-
- }
- HiliteMenu(0); // Unhighlight whatever MenuSelect or MenuKey hilited
- }
-
- //---------------------------------------------------------------------
-
- void InitMac( void )
- {
- Handle menuBar = nil;
-
- // we want to grow the heap as big as possble, or the text media handler may not
- // load the text track properly initially
-
- MaxApplZone();
- MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters();
-
- InitGraf (&qd.thePort);
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ((long)nil);
-
- if (!IsQuickTimeInstalled()) {
- CheckError(-1,"\pPlease install QuickTime and try again.");
- }
-
- CheckError( EnterMovies (), "\pEnterMovies failed" );
-
- // get the menu bar
-
- menuBar = GetNewMBar(128); // Read menus into menu bar, MBAR res id is 128
-
- if ( menuBar == nil )
- ExitToShell(); // if we dont have it then quit - your app
- // needs a dialog here
-
- SetMenuBar(menuBar); // Install menus
- DisposHandle(menuBar);
-
- AddResMenu(GetMHandle(mApple), 'DRVR'); // Add DA names to Apple menu, ID 128
-
- DrawMenuBar();
-
-
- gQuitFlag = false ; // set the flag for the main event loop
- }
-
-
- //---------------------------------------------------------------------
-
- void DoDrawCurrentText( WindowPtr theWindow )
- {
- GrafPtr savedPort ;
- Rect theRect;
- short h, v ;
- RGBColor savedFore, savedBack ;
- DocumentHandle aDocHdl ;
- char cMemTags ;
- TEHandle aDocTEH ;
-
- GetPort( &savedPort ) ;
- SetPort( (GrafPtr)theWindow ) ;
-
- aDocHdl = (DocumentHandle)GetWRefCon( theWindow ) ;
-
- if(aDocHdl == nil)
- return ;
-
- aDocTEH = (**aDocHdl).myTEH ;
- theRect = (**aDocTEH).viewRect ;
- TEUpdate( &theRect, aDocTEH ) ;
-
- // reset the port
- SetPort( savedPort ) ;
- }
-
-
-
- //---------------------------------------------------------------------
-
- pascal OSErr MyTextProc( Handle thisText,
- Movie thisMovie,
- short *displayFlag,
- long refcon) // contains a ref to the window for this movie
- {
- long totalSampleSize ;
- char *samplePtr = nil ;
- char *stylePtr = nil;
- char *textPtr = nil;
-
- short sampleSize ;
- short styleSize ;
- short textSize ;
-
- StScrpHandle scrapHdl = nil;
- DocumentHandle aDocHdl = nil;
- TEHandle myDocTEH = nil;
- GrafPtr savedPort ;
- Rect aRect ;
- OSType atomType ;
-
- char thisTextState ;
- SampleDescriptionHandle sampleDescriptionH = (SampleDescriptionHandle)NewHandle( 0 ) ;
-
-
- thisTextState = HGetState( thisText ) ;
- HLock( thisText ) ;
-
- GetPort( &savedPort ) ;
- SetPort((WindowPtr)refcon) ;
-
- aDocHdl = (DocumentHandle)GetWRefCon((WindowPtr)refcon) ;
- if( aDocHdl == nil )
- return noErr ;
-
- myDocTEH = (**aDocHdl).myTEH ;
-
- // both the text and the style information are included in the handle
- // get the size of the handle and the size of the text
-
- totalSampleSize = GetHandleSize(thisText); // this is the size of the total handle
- // which may contain styl information
-
- textSize = *(short*)(*thisText); // This will yield the actual size of the text
- textPtr = (char*)(*thisText + sizeof(short)); // yields a reference to the start of the text
-
-
- // get the default style for this sample
- {
- MovieController aController ;
- Movie aMovie ;
- Track aTrack ;
- OSErr theErr ;
-
- if((aController = (**aDocHdl).myController) != nil ) {
- if((aMovie = MCGetMovie( aController )) != nil ) {
-
- // get the text media track
- if((aTrack = GetFirstTrackOfType( aMovie, TextMediaType )) != nil )
- {
-
- Media aMedia ;
-
- TimeValue currentTime ;
- TimeValue interestingTime ;
- TimeValue mediaCurrentTime ;
- TimeValue theDuration ;
-
- Handle myData = NewHandle( 0 ) ;
-
- CheckError( GetMoviesError(), "\pfailed after GetFirstTrackOfType");
- aMedia = GetTrackMedia( aTrack ) ;
-
- // get the current movie time
- currentTime = GetMovieTime( aMovie, nil ) ;
- if( currentTime < 0 )
- CheckError( -1, "\pthis is an empty edit") ;
-
- // look back and find where this text starts
- GetTrackNextInterestingTime( aTrack, nextTimeEdgeOK | nextTimeMediaSample, currentTime,
- -kFix1, &interestingTime, nil ) ;
-
- currentTime = interestingTime ;
-
- GetTrackNextInterestingTime( aTrack, nextTimeEdgeOK | nextTimeMediaSample, currentTime,
- kFix1, &interestingTime, &theDuration ) ;
-
- mediaCurrentTime = TrackTimeToMediaTime( interestingTime, aTrack ) ;
-
- // mediaSampleStartTime contains the start of this sample
- theErr = GetMediaSample( aMedia,
- myData,
- 0L,
- nil,
- mediaCurrentTime,
- nil,
- nil,
- sampleDescriptionH,
- nil,
- 0L,
- nil,
- nil) ;
- }
- }
- }
- }
-
- if( sampleDescriptionH ) {
- scrapHdl = (StScrpHandle)NewHandle(sizeof(StScrpRec)) ;
- if( scrapHdl == nil )
- CheckError(MemError(), "\pCouldn't allocate memory for the style table" ) ;
- (**scrapHdl).scrpNStyles = 1 ;
- (**scrapHdl).scrpStyleTab[0] = (**((TextDescriptionHandle)sampleDescriptionH)).defaultStyle ;
-
- // this next item will force the style run to be in the window's fg color, we
- // may not want to force this, so remove this if you want to use colored text.
- (**scrapHdl).scrpStyleTab[0].scrpColor = ((CGrafPtr)(**myDocTEH).inPort)->rgbFgColor;
-
-
- // delete the previous contents of the TEH
- TESetSelect( 0, (**myDocTEH).teLength, myDocTEH );
- TEDelete( myDocTEH ) ;
-
- // insert the new text
- TEStylInsert( textPtr, textSize, scrapHdl, myDocTEH );
-
- DisposHandle((Handle)scrapHdl) ;
- }
- else {
- TESetText( textPtr, textSize, myDocTEH ) ;
- }
- // additional style information is in the format of a TE Style Scrap Record
- // there is other information here too. If there is more information
- // to get, then it will be appended as atoms. we will know that there
- // is more to look at if the size of the total sample is larger than the
- // size of the text sample.
- //
- // Each atom is
- // Atom Size - long (4bytes). This includes the size and type fields
- // Atom Type - OSType (4 Bytes)
- if( totalSampleSize > (textSize + sizeof(short))) {
-
- // step through whatever is in the handle
-
- for( samplePtr = (*thisText) + (textSize + sizeof(short)) ; // start at the atom after the text
- (samplePtr - (*thisText)) < totalSampleSize ; // make sure that we don't go off the end
- samplePtr += sampleSize ) // bump up the pointer
- {
- // get the sample size and do the best we can to validate with it
- if((sampleSize = *((long *) samplePtr)) < 0 || sampleSize > totalSampleSize) break;
- atomType = *((long *)(samplePtr + sizeof( long ))) ;
-
- // the only one we are interested in is the 'styl' atom
- // other types exist:
- // 'tbox' - shrunken text box atom
- // 'hlit' - highlighting atom
- // 'dlay' - scroll delay
- // 'hclr' - highlight color
- // see page 2-290 of Inside Macintosh: QuickTime for details
-
- switch( atomType ) {
-
- case 'styl':
- styleSize = sampleSize - (2*sizeof(long)) ;
- stylePtr = samplePtr + (2*sizeof(long)) ;
- scrapHdl = (StScrpHandle)NewHandle(styleSize);
-
- if (MemError() != noErr || scrapHdl == nil)
- return;
-
- BlockMove(stylePtr, *scrapHdl, styleSize);
-
- // and apply the style to the TERec
- SetStylScrap( 0, 32767, scrapHdl, false, myDocTEH ) ;
-
- break ;
-
- default:
- // you could parse the other types here
- break ;
-
- }
- }
- }
-
- // Invalidate the area of the TEH
-
- aRect = (**myDocTEH).viewRect ;
- InvalRect( &aRect ) ;
- // invalidate the control and set it so it is not enabled
-
- aRect = (**(**aDocHdl).myUpdateButton).contrlRect ;
- InvalRect( &aRect ) ;
-
- // disable the control
- (**aDocHdl).myTextHasBeenModified = false ;
-
- // just display whatever we would do on the movie
- *displayFlag = txtProcDefaultDisplay;
-
- HSetState(thisText,thisTextState);
-
- SetPort(savedPort) ;
-
- }
-
- //---------------------------------------------------------------------
- // main - entry point for the app.
-
- void main (void)
- {
- // initialise the managers
- InitMac() ;
-
- // initialise the appleevent hadlers
- InitAEStuff() ;
- RegisterMyEvents() ;
-
- // run 'til we die…
- MainEventLoop() ;
- }
-
- // command handlers
-
- //-----------------------------------------------------------------------
- // we don't do this, so tell whoever called us by returning
- // errAEEventNotHandled
-
- OSErr HandlePrintDoc( FSSpec *myFSS )
- {
- return errAEEventNotHandled ;
- }
-
- //-----------------------------------------------------------------------
-
- OSErr HandleOpenDoc( FSSpec *myFSS )
- {
- Movie aMovie = nil;
- short movieResFile;
- short movieResID = 0; // want the first movie in the file
- Str255 movieName;
- Boolean wasChanged;
-
- // attempt to open the movie file, and create a window
- OSErr err = OpenMovieFile (myFSS, &movieResFile, fsRdPerm );
-
- if (err == noErr) {
- err = NewMovieFromFile (&aMovie, movieResFile,
- &movieResID,
- movieName,
- newMovieActive, // flags
- &wasChanged);
- CloseMovieFile (movieResFile);
- if(err == noErr )
- DoCreateMovieWindow( aMovie ) ;
- }
-
- return err ;
- }
-
-